//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：UIFrameViewLayer
// *描述：用于层级管理的默认脚本
// *?2021 MYJL . All rights reserved.
//* ======================================================================
using System;
using System.Collections.Generic;
using UnityEngine;

namespace HDMCode.UIFrame
{
    public class UIFrameOpenLayer : IUIFrameViewLayer
    {
        List<UIDisplay> m_NeedHideUI = new List<UIDisplay>();

        public bool GetFocussingList(out List<UIDisplay> hideUIs)
        {
            hideUIs = m_NeedHideUI;
            return hideUIs.Count > 0;
        }
        /// <summary>
        /// 为打开的UI设置层级
        /// </summary>
        /// <param name="entityUI">当前UI</param>
        public void SetUILayer(UIDisplay entityUI)
        {
            WrapperType wrapperType = entityUI.wrapperType;
            ShowType showType = entityUI.showType;
            PierceType pierceType = entityUI.pierceType;
            BackGroundType backGroundType = entityUI.backGroundType;
            SetWrapperType(wrapperType,entityUI);
            SetShowType(wrapperType,showType,entityUI);
            SetPierceType(pierceType,entityUI);
            SetWrapperWall();
            SetBackGroundType(entityUI.backGroundType,entityUI.backGroundColor);
        }

        /// <summary>
        /// 设置UI层级
        /// </summary>
        /// <param name="wrapperType">UI类型</param>
        /// <param name="uIDisplay">UI对象</param>
        public void SetWrapperType(WrapperType wrapperType, UIDisplay uIDisplay)
        {
            switch (wrapperType)
            {
                case WrapperType.FullScreen:
                    uIDisplay.gameObject.transform.SetParent(HDMUI.Runtime.FullScreen.transform, false);
                    break;
                case WrapperType.Fixation:
                    uIDisplay.gameObject.transform.SetParent(HDMUI.Runtime.Fixation.transform, false);
                    break;
                case WrapperType.Dialog:
                    uIDisplay.gameObject.transform.SetParent(HDMUI.Runtime.Dialog.transform, false);
                    break;
                case WrapperType.inform:
                    uIDisplay.gameObject.transform.SetParent(HDMUI.Runtime.Inform.transform, false);
                    break;
            }
        }
        
        /// <summary>
        /// 设置UI显示方式
        /// </summary>
        /// <param name="wrapperType">UI窗体类型</param>
        /// <param name="showType">UI显示类型</param>
        /// <param name="uIDisplay">UI对象</param>
        public void SetShowType(WrapperType wrapperType, ShowType showType, UIDisplay uIDisplay)
        {
            switch (showType)
            {
                case ShowType.Focussing:
                    m_NeedHideUI = HDMUI.Runtime.FrameStore.GetAllOnDisplay();
                    break;
                case ShowType.Standard:
                    uIDisplay.transform.SetAsLastSibling();
                    break;
                case ShowType.Queue:
                    uIDisplay.transform.SetAsLastSibling();
                    break;
            }
        }

        /// <summary>
        /// 设置UI穿透方式
        /// </summary>
        /// <param name="pierceType">UI穿透类型</param>
        /// <param name="uIDisplay">UI对象</param>
        public void SetPierceType(PierceType pierceType, UIDisplay uIDisplay)
        {
            switch (pierceType)
            {
                case PierceType.Impenetrate:
                    HDMUI.Runtime.BackWall.image.raycastTarget = true;
                    break;
                case PierceType.Penetrate:
                    HDMUI.Runtime.BackWall.image.raycastTarget = false;
                    break;
            }
        }

        /// <summary>
        /// 设置UI背景颜色
        /// </summary>
        /// <param name="backGroundType">颜色类型</param>
        /// <param name="color">颜射值</param>
        public void SetBackGroundType(BackGroundType backGroundType, Color color,Action callBack=null)
        {
            switch (backGroundType)
            {
                case BackGroundType.Transparent:
                    //UIFrameRuntime.Get.backWall.wall.GetComponent<Image>().color = new Color(0,0,0,0);
                    HDMUI.Helper.AsyncSetColor(HDMUI.Runtime.BackWall.image, new Color(0, 0, 0, 0), callBack);
                    break;
                case BackGroundType.Translucent:
                    //UIFrameRuntime.Get.backWall.image.color = new Color(0f, 0f, 0f, 0.5f);
                    HDMUI.Helper.AsyncSetColor(HDMUI.Runtime.BackWall.image, new Color(0f, 0f, 0f, 0.5f), callBack);
                    break;
                case BackGroundType.Opaque:
                    //UIFrameRuntime.Get.backWall.image.color = new Color(0f, 0f, 0f, 1f);
                    HDMUI.Helper.AsyncSetColor(HDMUI.Runtime.BackWall.image, new Color(0f, 0f, 0f, 1f), callBack);
                    break;
                case BackGroundType.Custom:
                    //UIFrameRuntime.Get.backWall.image.color = color;
                    HDMUI.Helper.AsyncSetColor(HDMUI.Runtime.BackWall.image, color, callBack);
                    break;
            }
        }
        
        /// <summary>
        /// 为指定层级设置自动背景
        /// </summary>
        public void SetWrapperWall()
        {
            HDMUI.Runtime.BackWall.wall.gameObject.SetActive(false);
            var dialog = HDMUI.Runtime.Dialog.transform;
            var full = HDMUI.Runtime.FullScreen.transform;
            for (int i = dialog.childCount - 1; i >= 0; i--)
            {
                if (dialog.GetChild(i).gameObject.activeSelf)
                {
                    int siblingIndex = dialog.GetChild(i).GetSiblingIndex();
                    int newIndex = siblingIndex - 1;
                    if (newIndex < 0)
                        newIndex = 0;
                    HDMUI.Runtime.BackWall.wall.SetParent(dialog);
                    HDMUI.Runtime.BackWall.wall.SetSiblingIndex(newIndex);
                    HDMUI.Runtime.BackWall.wall.gameObject.SetActive(true);
                    return;
                }
            }
            for (int i = full.childCount - 1; i >= 0; i--)
            {
                if (full.GetChild(i).gameObject.activeSelf)
                {
                    int siblingIndex = full.GetChild(i).GetSiblingIndex();
                    int newIndex = siblingIndex - 1;
                    if (newIndex < 0)
                        newIndex = 0;
                    HDMUI.Runtime.BackWall.wall.SetParent(full);
                    HDMUI.Runtime.BackWall.wall.SetSiblingIndex(newIndex);
                    HDMUI.Runtime.BackWall.wall.gameObject.SetActive(true);
                    return;
                }
            }
        }

    }
}
